package com.liinxin.flac_management.page;

import com.liinxin.flac_management.controller.SongLabelController;
import com.liinxin.flac_management.entity.FileTextField;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class EditLabelUI {

    private Stage primaryStage;
    private Scene startScene;

    private SongLabelController songLabelController = new SongLabelController();

    public EditLabelUI() {
    }

    public EditLabelUI(Stage primaryStage, Scene startScene) {
        this.primaryStage = primaryStage;
        this.startScene = startScene;
    }

    public Scene getScene() {
        // 创建一个标签
        Label label = new Label("Flac文件添加标签");
        label.setFont(new Font("Arial", 20));

        Label pathLabel = new Label("请输入文件路径: ");
        pathLabel.setFont(new Font("Arial", 15));

        // 输入框
        FileTextField pathInput = new FileTextField();
        pathInput.setFont(new Font("Arial",20));

        // 创建添加标签按钮
        Button runButton = new Button("添加标签");
        runButton.setFont(new Font("Arial", 20));

        // 清空地址按钮
        Button clearButton = new Button("清空地址");
        clearButton.setFont(new Font("Arial", 20));

        // 创建一个返回按钮
        Button backButton = new Button("返回主页");
        backButton.setFont(new Font("Arial", 20));

        // 创建一个HBox布局管理器来包含按钮
        HBox buttonBox = new HBox(40);
        buttonBox.setAlignment(Pos.CENTER);
        // 将按钮添加到HBox布局管理器中
        buttonBox.getChildren().addAll(runButton, clearButton, backButton);

        // 添加标签按钮
        runButton.setOnAction(event -> {
            String path = pathInput.getText();
            System.out.println("添加标签: "+path);

            // 编辑标签
            try {
                songLabelController.editAllLabel(path);

                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setTitle("成功");
                alert.setHeaderText("修改成功");

                // 显示提示框并等待用户关闭
                alert.showAndWait();

                // pathInput.setText("");

            } catch (Exception e) {
                // 显示错误提示框
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("错误");
                alert.setHeaderText("路径错误");

                // 显示提示框并等待用户关闭
                alert.showAndWait();

                // 清空输入框数据
                pathInput.setText("");
            }
        });

        // 清空地址
        clearButton.setOnAction(event -> {
            System.out.println("清空地址");
            pathInput.setText("");
        });

        // 为返回按钮添加事件处理程序
        backButton.setOnAction(event -> {
            String text = pathInput.getText();
            System.out.println(text);

            primaryStage.setScene(startScene);
            primaryStage.show();
        });

        // 创建一个VBox布局管理器作为根布局
        VBox root = new VBox(15);
        root.getChildren().addAll(label, pathLabel, pathInput, buttonBox); // 将buttonBox替换原来的两个按钮

        // 创建一个场景并返回
        Scene scene = new Scene(root, 500, 500);
        return scene;
    }

}
